import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/dartLearn/language_learn.dart';
import 'package:flutter_app/openeye/utils/toast_utils.dart';
import 'package:flutter_app/openeye/widget/app_bar.dart';
import 'package:flutter_app/stateLearn/click_txt.dart';

class OtherAPITest extends StatefulWidget {
  const OtherAPITest({Key? key}) : super(key: key);

  @override
  _OtherAPITestState createState() => _OtherAPITestState();
}

class _OtherAPITestState extends State<OtherAPITest> {
  var widgetList = <Widget>[];

  @override
  Widget build(BuildContext context) {
    return materialPage(
        page: inputTestPage(),
        appBar: appBar("API测试页面"));
  }

  Widget createTestPage() {
    return Container(
      alignment: Alignment.center,
      child: const Text("api测试"),
    );
  }

  Widget createUpDataListPage() {
    return Column(
      children: [
        ClickTxt(
            info: "更新列表",
            onPressed: () {
              setState(() {
                widgetList = List.from(widgetList);
                widgetList.add(const Text("新添加的条目"));
              });
            }),
        Flexible(
          child: createListPage(),
        ),
      ],
    );
  }

  Widget createListPage() {
    for (int i = 0; i < 10; i++) {
      widgetList.add(GestureDetector(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Text('Row $i'),
        ),
        onTap: () {
          ToastUtils.showTip('Row: $i');
        },
      ));
    }
    return ListView.separated(
      itemCount: widgetList.length,
      itemBuilder: (context, index) {
        return widgetList[index];
      },
      separatorBuilder: (BuildContext context, int index) {
        return const Divider(
          height: 10,
          thickness: 3,
          indent: 10,
          endIndent: 10,
        );
      },
    );
  }
  var errorInfo;

  inputTestPage() {
    //获取输入框输入内容
    var textEditingController = TextEditingController();
    textEditingController.addListener(() {
      print("输入内容监听:${textEditingController.text}");
    });
    return Container(
      alignment:Alignment.center,
      child:  TextField(
        onSubmitted: (info){
          print("键盘确定按钮事件回调onSubmitted收到输入内容:$info");
          setState(() {
            if(isEmail(info)){
              errorInfo = null;
            }else{
              errorInfo = "输入错误";
            }
          });
        },
        decoration: InputDecoration(hintText: "提示文本",errorText: errorInfo),
        controller: textEditingController,
      ),
    );
  }

  bool isEmail(String info) {
    String emailRegexp =   r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$';
    var regExp = RegExp(emailRegexp);
    return regExp.hasMatch(info);
  }

}
